/**
 * Created by dukes on 2015/11/4.
 */
class DateScene extends eui.Component {
    private frameItem:FrameItem;
    constructor() {
        super();
        this.skinName = "skins.scene.DateSceneSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addedToStage, this);
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
        this.name = "datescene";
        this.frameItem = new FrameItem();
        this.frameItem.callFun = this.updateTime;
        this.frameItem.callObj = this;
    }
    public selectedItem:FollowItemRender;
    public onAttentionBack(type){
        if( type == 1){
            if( this.curType == 0){
                this.provider.source = Model.attentionList;
                if( Model.attentionList.length){
                    this.followList.selectedIndex = 0;
                    window["__visit"](Model.attentionIdList[0]);
                }
                this.info_group.visible = Model.attentionList.length != 0;
                PetMgr.getInstance().cleanMain();
            }else{
                this.selectedItem.dataChanged();
            }
        }else if( type == 0){
            //if( Model.strangerList && Model.strangerList.length){
                //var len = Model.strangerList.length;
                //for(var i = 0; i < len; i++){
                //    if(Model.strangerList[i] == this.followList.selectedItem){
                //        Model.strangerList.splice(i,1);
                //        break;
                //    }
                //}
                //this.provider.source = Model.strangerList;
                //this.followList.selectedIndex = 0;
                this.selectedItem.dataChanged();
            //}
        }
        if( this.provider.source.length == 0){
            this.curVisitId = -1;
        }
        this.updateInfo();
        //this.selectedItem.dataChanged();
        this.group_limit.text = Model.attentionIdList.length+"p"+Model.userExt.maxAttentionNum;
        //GetUIAssetsNum(Model.attentionIdList.length+"/"+Model.userExt.maxAttentionNum,this.group_limit);
    }
    private curType:number=0;
    public updateType(type = 0,isForce=false,noNeedVisit:boolean=false){
        this.curVisitId = -1;
        if(!isForce){
            if( type == 0 ){
                //if( !Model.attentionList){
                //    window["__lattention"]();
                //}else{
                    this.provider.source = Model.attentionList;
                    this.info_group.visible = Model.attentionList.length != 0;
                    if(Model.attentionList.length != 0 && this.curVisitId != Model.attentionList[0].attentionId){
                        window["__visit"](Model.attentionList[0].attentionId, false);
                    }else if(Model.attentionList.length == 0){
                        PetMgr.getInstance().cleanMain();
                    }
                    this.followList.selectedIndex = 0;
                //}
            }else if( type == 1){
                if( Model.strangerList){
                    this.provider.source = Model.strangerList;
                    this.info_group.visible = Model.strangerList.length != 0;
                    if(Model.strangerList.length != 0 && this.curVisitId != Model.strangerList[0].userId){
                        window["__visit"](Model.strangerList[0].userId, false);
                        PetMgr.getInstance().cleanMain();
                    }else if(Model.strangerList.length == 0){
                        PetMgr.getInstance().cleanMain();
                    }
                    this.followList.selectedIndex = 0;
                }
            }

        }else{
            this.followList.selectedIndex = 0;
            if(this.curVisitId != this.provider.source[0].userId){
                window["__visit"](this.provider.source[0].userId,true);
                PetMgr.getInstance().cleanMain();
            }
        }

        this.img_search.visible = this.img_change_group.visible = type == 1;
        this.curType = type;
        if(this.curType == 1){
            if( Model.userExt.foreignPillageCdTime > Model.SYSTIME){//} && !FrameMgr.getInstance().isUnderControl(this.name)){
                //FrameMgr.getInstance().addControll(this.frameItem);
                this.rob_time_left.visible = this.img_lab_rob.visible = false;
                this.group_rob_timer.visible = true;
            }
            else
                this.updateInfo();
        }else{
            //if( FrameMgr.getInstance().isUnderControl(this.name)){
            //    FrameMgr.getInstance().delController(this.name);
            //}
            this.rob_time_left.visible = this.img_lab_rob.visible = true;
            this.group_rob_timer.visible = false;
        }
        if( this.provider.source.length == 0){
            this.updateInfo();
        }
    }
    private updateDateInfo(){
        if( PetMgr.getInstance().curIndatePetId != 0){
            var pet:Pet = Model.getPetById(PetMgr.getInstance().curIndatePetId);
            this.lab_name.text = pet.name;
            this.img_pet.source = GameConst.Res_Root+"resource/assets/petpreview/"+pet.petId+".png";
            this.img_male.visible = pet.sex == 0;
            this.img_female.visible = pet.sex == 1;
            this.img_money.source = pet.type == Model.GOOD_TYPE_CAT ? "img_money_cat" : "img_money_dog";
        }
        if(this.curType == 1){
            if( Model.userExt.foreignPillageCdTime > Model.SYSTIME ){//&& !FrameMgr.getInstance().isUnderControl(this.name)){
                //FrameMgr.getInstance().addControll(this.frameItem);
                this.rob_time_left.visible = this.img_lab_rob.visible = false;
                this.group_rob_timer.visible = true;
            }
            else
                this.updateInfo();
        }
        if( this.curVisitId == 0) return;
        if(this._curPet && this._curPet == PetMgr.getInstance().othersPet){
            PetMgr.getInstance().showOthersPet();
        }
    }

    public updateWarRelated(){
        var pet:Pet = Model.getPetById(PetMgr.getInstance().curIndatePetId);
        var war = Model.GetPerAttrs(PetMgr.getInstance().curIndatePetId);
        var tempAttackAdd = 0;
        var tempDefenseAdd = 0;
        //var logo = GuildMgr.getInstance().guildLogo;
        //if( logo){
        //    tempAttackAdd += logo.attrAttack +logo.addtionAttack;
        //    tempDefenseAdd += logo.attrDefense +logo.addtionDefense;
        //}
        var moodAdd = pet.mood/50;
        this.group_attack.text = ( Math.round(war.attack*moodAdd) + tempAttackAdd )+"";
        this.group_defense.text = (Math.round(war.defense*moodAdd) + tempDefenseAdd) +"";
    }

    private info;
    public curVisitId = -1;
    public onDateInfoBack(){
        if( !this.parent || this.curType != 1){
            return;
        }
        this.provider.source = Model.strangerList;
        this.info_group.visible = Model.strangerList.length != 0;
        //group_date_times
        if( Model.strangerList.length == 0)
            return;
        var id = Model.strangerList[0].userId;
        if( this.curVisitId == id ){
            return;
        }
        if( Model.strangerList.length != 0){
            this.followList.selectedIndex = 0;
            window["__visit"](id, false);
        }
    }
    public updateInfo(){
        if( this.curVisitId == -1){
            if( this.curType == 0){
                this.group_date_times.text = (Model.userExt.maxAttentionDateTimes-Model.userExt.attentionDateTimes)+"";
                this.rob_time_left.text = (Model.userExt.maxAttentionPillageTimes-Model.userExt.attentionPillageTimes)+"";
            }else{
                this.group_date_times.text = (Model.userExt.maxForeignDateTmes-Model.userExt.foreignDateTmes)+"";
                if( Model.userExt.foreignPillageFreeCleanTimes > Model.userExt.foreignPillageCleanTimes){
                    this.rob_time_left.text = (Model.userExt.foreignPillageFreeCleanTimes-Model.userExt.foreignPillageCleanTimes)+"";
                }else{
                    if( Model.userExt.foreignPillageCdTime > Model.SYSTIME){
                        //if( !FrameMgr.getInstance().isUnderControl(this.name)){
                        //    FrameMgr.getInstance().addControll(this.frameItem);
                        //}
                        this.group_rob_timer.visible = true;
                       this.img_lab_rob.visible = this.rob_time_left.visible = false;
                    }else{
                        //if( FrameMgr.getInstance().isUnderControl(this.name)){
                        //    FrameMgr.getInstance().delController(this.name);
                        //}
                        this.rob_time_left.text = "1";
                    }
                }

            }
            this.updateItems({});
        }else{
            if( Model.attentionIdList.indexOf(this.curVisitId)!=-1){
                //if( FrameMgr.getInstance().isUnderControl(this.name)){
                //    FrameMgr.getInstance().delController(this.name);
                    this.rob_time_left.visible = this.img_lab_rob.visible = true;
                    this.group_rob_timer.visible = false;
                //}
                this.group_date_times.text = (Model.userExt.maxAttentionDateTimes-Model.userExt.attentionDateTimes)+"";
                this.rob_time_left.text = (Model.userExt.maxAttentionPillageTimes-Model.userExt.attentionPillageTimes)+"";
            }else{
                this.group_date_times.text = (Model.userExt.maxForeignDateTmes-Model.userExt.foreignDateTmes)+"";
                if( Model.userExt.foreignPillageFreeCleanTimes > Model.userExt.foreignPillageCleanTimes){
                    this.rob_time_left.text = (Model.userExt.foreignPillageFreeCleanTimes-Model.userExt.foreignPillageCleanTimes)+"";
                }else{
                    if( Model.userExt.foreignPillageCdTime > Model.SYSTIME){
                        //if( !FrameMgr.getInstance().isUnderControl(this.name)){
                        //    FrameMgr.getInstance().addControll(this.frameItem);
                        //}
                        this.group_rob_timer.visible = true;
                        this.img_lab_rob.visible = this.rob_time_left.visible = false;
                    }else{
                        //if( FrameMgr.getInstance().isUnderControl(this.name)){
                        //    FrameMgr.getInstance().delController(this.name);
                        //}
                        this.rob_time_left.text = "1";
                    }
                }
            }
        }
    }
    public onDateRefreshBack(){
        this.provider.source = Model.strangerList;
        if( Model.strangerList.length != 0){
            this.followList.selectedIndex = 0;
            this.onSelected(null);
        }
    }
    public onIndateBack(){
        this.updateDateInfo();
    }

    private onChangerLeader(evt:egret.TouchEvent){
        DateSelectPanel.getInstance().show();
    }

    private onZan(evt:egret.TouchEvent){
        var item =this.followList.selectedItem;
        if( Model.userExt.praiseTimes >= Model.userExt.maxPraiseTimes){
            //ShowHint("今日点赞次数已经用完");
            RemindPanel.getInstance().showContent("今日点赞次数已用完，明早6点重置。\n是否花费59钻石购买99次点赞次数？未使用完的次数明日清空。",this, this.onBuyZan);
            return;
        }
        if( item.attentionId)
            window["__praise"](item.attentionId,false);
        else
            window["__praise"](item.userId,false);
    }

    private onBuyZan(){
        if( Model.me.diamond < 59){
            ShowHint("钻石不足");
            BuyDiamondPanel.getInstance().show();
        }else{
            window["__resetPraise"]();
        }
    }

    private onSearch(evt:egret.TouchEvent){
        SearchPanel.getInstance().show();
    }

    private onRefresh(evt:egret.TouchEvent){
        window["__dateRefresh"]();
    }

    private onDate(evt:egret.TouchEvent){
        window["__date"](this._curPet.userId);
        PetMgr.getInstance().othersPet = this._curPet;
    }

    public onRob(evt:egret.TouchEvent){
        if( evt)
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        var userId = this._curPet.userId;
        if(userId == Model.me.id){
            ShowHint("您不能抢自己!");
            return;
        }

        if( Model.robedList.indexOf(userId) != -1){
            ShowHint("已抢");
            return;
        }
        var isFriend = Model.attentionIdList.indexOf(userId) != -1;
        if( isFriend ){
            if(Model.userExt.attentionPillageTimes >= Model.userExt.maxAttentionPillageTimes){
                ShowHint("抢毛次数用完，早6点重置");
                return;
            }
        }else{
            if( Model.userExt.foreignPillageCdTime > Model.SYSTIME){
                var cost = Math.ceil((Model.userExt.foreignPillageCdTime - Model.SYSTIME)/(1000*60));
                RemindPanel.getInstance().showContent("是否花费"+cost+"钻石清除本次冷却时间，清除冷却时间后可立即抢毛",this,this.onBuyCD)
                return;
            }
        }
        var buff = BuffMgr.getInstance().getBuffById(BuffMgr.NO_WAR_BUFF);
        if(buff && buff.endTime > Model.SYSTIME){
            RemindPanel.getInstance().showContent("您当前处于免战状态，一旦发起挑战将导致免战倒计时立即结束，是否继续挑战？",this, this.onClearBuff);
            return;
        }

        if( this.noWarBuff != 0 && this.noWarBuff > Model.SYSTIME){
            RemindPanel.getInstance().showContent("对方高挂免战牌，不能迎战");
            return;
        }

        if( this._shieldBuff > Model.SYSTIME){
            ForceRobPanel.getInstance().show( this._shieldBuff,userId);
            return;
        }
        window["__pillage"](userId);
    }

    private onClearBuff(){
        window["__clearPeaceBuff"](0,this._curPet.userId);
    }

    private onBuyCD(){
        var cost = Math.ceil((Model.userExt.foreignPillageCdTime - Model.SYSTIME)/(1000*60));
        if( cost <= Model.me.diamond){
            window["__cleancd"]();
        }else{
            //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
            BuyDiamondPanel.getInstance().show();
        }
    }

    public onPillageStartBack(hehe2){
        this._curPet.mood = hehe2.mood;
        this._robHair.hair = hehe2.hair;
        Model.userExt = hehe2.userExtendInfo;
        Model.robedList.push(hehe2.petWar.userId);
        this.img_rob.visible = false;
        this.img_robed.visible = true;
        this.updateInfo();
        SceneMgr.getInstance().switchScene(SceneMgr.SCENE_BATTLE,[hehe2.petWar,hehe2.bePillagePet, this._robHair.hair,0,hehe2.bePillageHarm,hehe2.pillageHarm,]);
    }

    private onRocket(evt:egret.TouchEvent){
        RemindPanel.getInstance().showContent("是否花费888钻石购买全服火箭赠送给【"+this._curPet.userName+"】",this, this.onRocketOk);
    }

    private onChat(evt:egret.TouchEvent){
        var data = this.followList.selectedItem;
        var obj = {name:data.userName || data.attentionName, userId:data.attentionId||data.userId,figureurl:data.attentionFigureurl||data.userFigureurl};
        //ChatMgr.getInstance().addChatList(obj);
        SceneMgr.getInstance().switchScene(SceneMgr.SCENE_CHAT_P,[obj]);
    }

    private onRocketOk(){
        window["__handsel"](this._curPet.userId,1111);
    }

    private firstTime = true;
    private addedToStage(evt:egret.Event){
        //if(  !Model.attentionIdList ){
        //    window["__lattention"]();
        //}else{
            if( this.firstTime){
                this.provider.source = Model.attentionList;
                if( Model.attentionIdList.length != 0){
                    this.followList.selectedIndex = 0;
                    window["__visit"](Model.attentionList[0].attentionId, false);
                }else{
                    this.info_group.visible = false;
                }
                this.firstTime = false;
                this.updateInfo();
            }
        //}
        if( !Model.petAttributes){
            window["__iwars"](PetMgr.getInstance().curPet.petId);
        }else{
            this.updateWarRelated();
        }
        this.group_limit.text = Model.attentionIdList.length+"p"+Model.userExt.maxAttentionNum;
        //GetUIAssetsNum(Model.attentionIdList.length+"/"+Model.userExt.maxAttentionNum,this.group_limit);
        this.img_change_leader.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangerLeader, this);
        this.img_zan.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onZan, this);
        this.followList.addEventListener(egret.Event.CHANGE, this.onSelected, this);
        this.img_search.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSearch, this);
        this.img_change_group.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRefresh, this);
        this.img_date.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDate, this);
        this.img_rob.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRob, this);
        this.img_rocket.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRocket, this);
        this.img_chat.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChat, this);
        this.updateDateInfo();
        //this.updateInfo();

    }
    private removedFromStage(evt:egret.Event){
        this.img_change_leader.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChangerLeader, this);
        this.img_zan.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onZan, this);
        this.img_search.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSearch, this);
        this.img_change_group.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRefresh, this);
        this.followList.removeEventListener(egret.Event.CHANGE, this.onSelected, this);
        this.img_date.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onDate, this);
        this.img_rob.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRob, this);
        this.img_rocket.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onRocket, this);
        this.img_chat.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onChat, this);
        PetMgr.getInstance().cleanMain();
    }

    private onSelected(evt:egret.Event){
        var item = this.followList.selectedItem;
        if( !item.attentionId && item.userId && this.curVisitId == item.userId) return;
        if( this.curVisitId == item.attentionId) return;
        if( !item.attentionId)
            window["__visit"](item.userId,false);
        else if( item.attentionId)
            window["__visit"](item.attentionId,false);
        SoundMgr.getInstance().playEffect(SoundMgr.CHOSE);
    }
    public onDateBack(){
        this.img_date.visible = Model.datedList.indexOf(this._curPet.userId) == -1;
        this.img_dated.visible = Model.datedList.indexOf(this._curPet.userId) != -1;
        this.img_rob.visible = Model.robedList.indexOf(this._curPet.userId) == -1;
        this.img_robed.visible = Model.robedList.indexOf(this._curPet.userId) != -1;
        this.updateInfo();
        this.updateItems(this.ornaments);
    }

    private _curPet:Pet;
    private _curWar:any;
    private _robHair:any;
    private _shieldBuff:number;
    public onVisitBack(pet,war,popularity,ornaments,box,hair,isSearch,canRob,peaceCardBuff,shieldTime,logoId){
        this._curPet = new Pet(pet);
        this._curWar = war;
        this._robHair = hair;
        this.curVisitId = pet.userId;
        var moodAdd = pet.mood/50;
        this.group_attack_info.text = Math.round(war.attack*moodAdd)+"";
        this.group_speed_i.text = war.speed+"";
        this.group_defense_info.text = Math.round(war.defense*moodAdd)+"";
        this.group_hardness.text = war.difficulty+"";
        this.group_can_rob.text = GetNumString(canRob,false,true);
        this.group_zan.text = popularity.popularity+"";

        this.img_date.visible = Model.datedList.indexOf(pet.userId) == -1;
        this.img_dated.visible = Model.datedList.indexOf(pet.userId) != -1;
        this.img_rob.visible = Model.robedList.indexOf(pet.userId) == -1;
        this.img_robed.visible = Model.robedList.indexOf(pet.userId) != -1;

        this.img_head.source = pet.userFigureurl;

        if( Model.me.platform != PlatformEnum.QQBROWSER){
            this.img_head.source =  pet.userFigureurl;
        }else{
            this.img_head.source = RES.getRes("default_head_"+(pet.userId%5));
        }
        PetMgr.getInstance().othersPet = this._curPet;
        PetMgr.getInstance().showOthersPet();
        this.updateItems(ornaments);
        if(  this.provider.source.length == 1 && this.provider.source[0]["userName"] ==""){
            var hehe =  this.provider.source[0];
            hehe.userName = pet.userName;
            this.provider.source[0]=hehe;
        }else if( isSearch){
            this.provider.source = [pet];
            this.followList.selectedIndex = 0;
            // var target = GetoutScene.getInstance().fuck_tab;
            // var oldIndex =  target.selectedIndex;
            // var newIndex = Model.attentionIdList.indexOf(pet.userId) == -1 ? 1 : 0;
            // if( oldIndex != newIndex){
            //     target.selectedIndex = newIndex;
            //     GetoutScene.getInstance().updateLabels();
            //     this.updateType(newIndex,true);
            // }
        }
        this.updateInfo();
        this.updateNoWarBuff(peaceCardBuff);
        this.updateShieldBuff(shieldTime);
        FrameMgr.getInstance().addControll(this.frameItem);
        this.img_guild_logo.visible = logoId != 0;
        if( logoId != 0){
            this.img_guild_logo.source = GameConst.Res_Root+"resource/assets/medals/"+logoId+".png";
        }
    }

    private noWarBuff:number = 0;
    public updateNoWarBuff(peaceCardBuff){
        this.noWarBuff = peaceCardBuff;
        if(this.noWarBuff != 0){
            this.img_no_war.visible = this.time_no_war.visible = true;
        }else{
            this.img_no_war.visible = this.time_no_war.visible = false;
        }
    }

    public updateShieldBuff(buffTime){
        this._shieldBuff = buffTime;
        this.img_shield.visible = buffTime != 0;
    }

    public forceDisplay(data:any){//不为空 来自回访  为空 来自pk按钮
        // var target = GetoutScene.getInstance().fuck_tab;
        // var oldIndex =  target.selectedIndex;
        // if( data == null){
        //     var newIndex = 1;
        //     if( oldIndex != newIndex){
        //         target.selectedIndex = newIndex;
        //         var event = egret.Event.create(eui.IndexChangeEvent,'change');
        //         event.oldIndex =  oldIndex;
        //         event.newIndex = newIndex;
        //         var result = target.dispatchEvent(event);
        //         egret.Event.release(event);
        //     }else{
        //         this.updateType(this.curType,true);
        //         GetoutScene.getInstance().updateLabels();
        //     }
        // }else{
        //     this.provider.source = [data];
        //     var target = GetoutScene.getInstance().fuck_tab;
        //     var newIndex = Model.attentionIdList.indexOf(data.userId) == -1 ? 1 : 0;
        //     if( oldIndex != newIndex){
        //         target.selectedIndex = newIndex;
        //         var event = egret.Event.create(eui.IndexChangeEvent,'change');
        //         event.data = 1;
        //         event.oldIndex =  oldIndex;
        //         event.newIndex = newIndex;
        //         var result = target.dispatchEvent(event);
        //         egret.Event.release(event);
        //     }else{
        //         this.updateType(this.curType,true);
        //         GetoutScene.getInstance().updateLabels();
        //     }
        //     this.info_group.visible = true;
        // }
    }

    private getIndexInList(userId):number{
        var arr = this.provider.source;
        var len = arr.length;
        for(var i = 0 ; i < len; i++){
            if( arr[i] instanceof Pet){
                if( arr[i].userId == userId){
                    return i;
                }
            }else{
                if( arr[i].attentionId == userId){
                    return i;
                }
            }
        }
        return -1
    }

    public onPraiseBack(info, popularity){
        this.group_zan.text = popularity.popularity+"";
        Model.userExt = info;
    }

    private updateTime(){
        var remove:boolean = false;

        if( (this.curVisitId == -1 && this.curType == 1) || (this.curVisitId != -1 && Model.attentionIdList.indexOf(this.curVisitId) == -1)){
            var timegap = Model.userExt.foreignPillageCdTime - Model.SYSTIME;
            if( timegap > 0){
                var timestr = GetTimeStr(timegap,2,true);
                this.group_rob_timer.text = timestr;
            }else{
                remove = true;
                this.group_rob_timer.text = "1";
                this.rob_time_left.visible = this.img_lab_rob.visible = true;
                this.group_rob_timer.visible = false;
            }
        }else{
            remove = true;
        }


        if( this.noWarBuff != 0){
            timegap = this.noWarBuff - Model.SYSTIME;
            if( timegap > 0){
                timestr = GetTimeStr(timegap,2,true);
                this.time_no_war.text = timestr;
                remove = false;
            }else{
                this.img_no_war.visible = this.time_no_war.visible = false;
            }
        }else{
            this.img_no_war.visible = this.time_no_war.visible = false;
        }

        if( this._shieldBuff != 0 ){
            timegap = this._shieldBuff - Model.SYSTIME;
            if( timegap > 0){
                timestr = GetTimeStr(timegap,2,true);
                remove = false;
            }else{
                this.img_shield.visible = false;
            }
        }else{
            this.img_shield.visible = false;
        }

        if( remove ){
            FrameMgr.getInstance().delController(this.name);
        }
    }

    private getOriginal(classify:number,arr:any[]){
        if( !arr || arr.length == 0 ) return 0;
        var dec:any;
        var len = arr.length;
        for( var i = 0 ; i < len; i++){
            dec = arr[i];
            if( dec.classify == classify){
                return dec.goodsId;
            }
        }
        return 0;
    }
    public ornaments:any;
    public updateItems(ornaments){
        this.ornaments = ornaments;
        if( ornaments == null) return;
        var type = this.getOriginal(Model.DEC_BG,ornaments);
        PetMgr.getInstance().changeBg(type);

        type = this.getOriginal(Model.DEC_NEST,ornaments);
        this.img_nest.visible = type != 0;
        if( type != 0)
            this.img_nest.source = GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png";

        type = this.getOriginal(Model.DEC_TOY,ornaments);
        this.img_doll.visible = type != 0;
        if( type != 0)
            this.img_doll.source = GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png";

        type = this.getOriginal(Model.DEC_FRAME,ornaments);
        this.img_head.visible = this.img_frame.visible = type != 0;
        if( type != 0)
            this.img_frame.source = GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png";

        type = this.getOriginal(Model.DEC_HAT,ornaments);
        if( type != 0)
            RES.getResByUrl(GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png",this.onHatComplete,this);
        else
            AnimationController.getInstance().setSlotDisplay("others","tou",false);

        type = this.getOriginal(Model.DEC_GLASS,ornaments);
        if( type != 0)
            RES.getResByUrl(GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png",this.onGlassComplete,this);
        else
            AnimationController.getInstance().setSlotDisplay("others","bozi",false);

        type = this.getOriginal(Model.DEC_NECKLACE,ornaments);
        if( type != 0)
            RES.getResByUrl(GameConst.Res_Root+"resource/assets/decorates/"+type+ ".png",this.onNeckComplete,this);
        else
            AnimationController.getInstance().setSlotDisplay("others","yanjing",false);
    }

    private onHatComplete(hehe){
        AnimationController.getInstance().setSlotDisplay("others","tou",true,hehe);
    }
    private onGlassComplete(hehe){
        AnimationController.getInstance().setSlotDisplay("others","yanjing",true,hehe);
    }
    private onNeckComplete(hehe){
        AnimationController.getInstance().setSlotDisplay("others","bozi",true,hehe);
    }

    private provider:eui.ArrayCollection;
    public img_doll:eui.Image;
    public img_frame:eui.Image;
    public img_nest:eui.Image;
    public img_prize:eui.Image;
    public group_zan:eui.BitmapLabel;
    public img_chat:eui.Image;
    public img_dated:eui.Image;
    public img_date:eui.Image;
    public img_robed:eui.Image;
    public img_rob:eui.Image;
    public img_rocket:eui.Image;
    public img_female:eui.Image;
    public img_male:eui.Image;
    public img_change_leader:eui.Image;
    public img_money:eui.Image;
    public img_pet:eui.Image;
    public img_zan:eui.Image;
    public group_attack:eui.BitmapLabel;
    public group_defense:eui.BitmapLabel;
    public zan_need:eui.BitmapLabel;
    public lab_name:eui.Label;
    public group_attack_info:eui.BitmapLabel;
    public group_defense_info:eui.BitmapLabel;
    public group_speed_i:eui.BitmapLabel;
    public group_can_rob:eui.BitmapLabel;
    public group_hardness:eui.BitmapLabel;
    public group_date_times:eui.BitmapLabel;
    public group_limit:eui.BitmapLabel;
    public followList:eui.List;
    public img_change_group:eui.Image;
    public img_search:eui.Image;
    public rob_time_left:eui.BitmapLabel;
    public info_group:eui.Group;
    public group_rob_timer:eui.BitmapLabel;
    public img_lab_rob:eui.Image;
    public img_head:eui.Image;
    public img_no_war:eui.Image;
    public time_no_war:eui.BitmapLabel;
    public img_shield:eui.Image;
    public img_guild_logo:eui.Image;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_prize") {
            this.img_prize = instance;
        }else if( name == "img_guild_logo"){
            this.img_guild_logo = instance;
        }else if (name == "group_zan") {
            this.group_zan = instance;
        }else if (name == "img_shield") {
            this.img_shield = instance;
        }else if (name == "followList") {
            this.followList = instance;
            this.followList.itemRenderer = FollowItemRender;
            this.provider = new eui.ArrayCollection();
            this.followList.dataProvider = this.provider;
        }else if (name == "img_chat") {
            this.img_chat = instance;
        } else if (name == "img_dated") {
            this.img_dated = instance;
        }else if (name == "img_date") {
            this.img_date = instance;
        }else if (name == "img_robed") {
            this.img_robed = instance;
        }else if (name == "img_rob") {
            this.img_rob = instance;
        }else if (name == "img_rocket") {
            this.img_rocket = instance;
        } else if (name == "img_female") {
            this.img_female = instance;
        }else if (name == "img_male") {
            this.img_male = instance;
        }else if (name == "img_change_leader") {
            this.img_change_leader = instance;
        }else if (name == "img_money") {
            this.img_money = instance;
        }else if (name == "img_pet") {
            this.img_pet = instance;
        } else if (name == "group_attack") {
            this.group_attack = instance;
        }else if (name == "group_defense") {
            this.group_defense = instance;
        }else if (name == "zan_need") {
            this.zan_need = instance;
        }else if (name == "lab_name") {
            this.lab_name = instance;
        }else if (name == "group_attack_info") {
            this.group_attack_info = instance;
        } else if (name == "group_defense_info") {
            this.group_defense_info = instance;
        }else if (name == "group_speed_i") {
            this.group_speed_i = instance;
        }else if (name == "group_can_rob") {
            this.group_can_rob = instance;
        }else if (name == "group_hardness") {
            this.group_hardness = instance;
        }else if (name == "group_date_times") {
            this.group_date_times = instance;
            this.group_date_times.textAlign = "center";
        }else if (name == "group_limit") {
            this.group_limit = instance;
            this.group_limit.textAlign = "center";
        }else if (name == "img_zan") {
            this.img_zan = instance;
        }else if (name == "img_search") {
            this.img_search = instance;
        }else if (name == "img_change_group") {
            this.img_change_group = instance;
        }else if (name == "rob_time_left") {
            this.rob_time_left = instance;
            this.rob_time_left.textAlign = "center";
        }else if (name == "img_doll") {
            this.img_doll = instance;
        }else if (name == "img_frame") {
            this.img_frame = instance;
        }else if (name == "img_nest") {
            this.img_nest = instance;
        }else if( name == "info_group"){
            this.info_group = instance;
        }else if( name == "img_lab_rob"){
            this.img_lab_rob = instance;
        }else if( name == "group_rob_timer"){
            this.group_rob_timer = instance;
            this.group_rob_timer.textAlign = "center";
        }else if( name == "img_head"){
            this.img_head = instance;
        }else if( name == "time_no_war"){
            this.time_no_war = instance;
            this.time_no_war.textAlign = "center";
        }else if( name == "img_no_war"){
            this.img_no_war = instance;
        }
    }
    private static _instance:DateScene;
    public static getInstance():DateScene{
        if( DateScene._instance == null )
            DateScene._instance = new DateScene();
        return DateScene._instance;
    }
}